package main

import (
	"fmt"
	"strconv"
	"time"
)

func generate(ch chan<- int) {
	for i := 2; ; i++ {
		ch <- i
	}
}

func filter(in <-chan int, out chan<- int, prime int, base int) {
	for {
		i := <-in
		fmt.Println("序号：" + strconv.Itoa(base) + "内部素数：" + strconv.Itoa(prime) + " 内部收到：" + strconv.Itoa(i))
		if i%prime != 0 {
			fmt.Printf("%v 不能被 %v 整除 \n", i, prime)
			out <- i
			time.Sleep(2 * time.Second)
		}
	}
}

func main() {
	defaultChan := make(chan int)
	go generate(defaultChan)
	for i := 0; i < 5; i++ {
		fmt.Println("##开始:" + strconv.Itoa(i))
		prime := <-defaultChan
		fmt.Println("##素数:" + strconv.Itoa(prime))
		outChan := make(chan int)
		go filter(defaultChan, outChan, prime, i)
		defaultChan = outChan
	}
	fmt.Println("结束素数计算")
	for {
		time.Sleep(1 * time.Second)
	}
}
